package com.ruoyi.project.zxbd.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.zxbd.mapper.BdGoodsLineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物料产品Controller
 *
 * @author zx
 * @date 2023-05-29
 */
@RestController
@RequestMapping("/zxbd/goodsLine")
public class BdGoodsLineController extends BaseController {
    @Autowired
    private BdGoodsLineMapper bdGoodsLineMapper;

    @PreAuthorize("@ss.hasPermi('zxbd:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = bdGoodsLineMapper.selectBdGoodsLineList(pd);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('zxbd:goods:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code) {
        PageData pageData = bdGoodsLineMapper.selectBdGoodsLineByCode(code);
        if (pageData.containsKey("attr_value")) {
            Object attr_value = pageData.get("attr_value");
            String[] split = attr_value.toString().replace("[", "").replace("]", "").replaceAll("\"", "").split(",");
            List<String> collect = Arrays.stream(split).collect(Collectors.toList());
            pageData.put("attr_value", collect);
        }
        return success(pageData);
    }


    @PreAuthorize("@ss.hasPermi('zxbd:goods:query')")
    @PutMapping()
    public AjaxResult updateLine(@RequestBody PageData pd) {
        Object attr_name = pd.get("attr_name");
        if (attr_name != null) {
            String json = JSON.toJSONString(attr_name);
            pd.put("attr_name", json);
        }
        return success(bdGoodsLineMapper.updateBdGoodsLine(pd));
    }

    @PreAuthorize("@ss.hasPermi('zxbd:goods:query')")
    @PostMapping()
    public AjaxResult addGoodsLine(@RequestBody PageData pd) {
        String num = bdGoodsLineMapper.selectNum(pd.get("parent_code").toString());
        int anInt = Integer.parseInt(num);
         num = anInt + 1 + "";
        if (num.length() == 1) {
            num = 0 + num;
        }
        pd.put("code", pd.get("parent_code").toString() + num);
        Object attr_name = pd.get("attr_name");
        String json = JSON.toJSONString(attr_name);
        pd.put("attr_name", json);
        return success(bdGoodsLineMapper.insertBdGoodsLine(pd));
    }

    @GetMapping(value = "/value")
    public AjaxResult getGoodsValue() {
        return success(bdGoodsLineMapper.selectBdGoodsValue());
    }

    @DeleteMapping("/{ids}")
    public AjaxResult deleteGoodsLine(@PathVariable String[] ids){
        return success(bdGoodsLineMapper.deleteBdGoodsLineByCodes(ids));
    }

}
